package BUCF;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.travel.core.AbstractDao;

public class UserCF {
    private String source;
    private int n;
    private List<user_sim> uslist = new ArrayList<>();

    public UserCF() {
    }

    public UserCF(String source, int n) {
        this.source = source;
        this.n = n;
    }

    public Set<String> getFavoriteSpot() {
        Set<String> favoriteSpots = new HashSet<String>();

        List<user_sim> temp_uslist = new ArrayList<user_sim>();
        Map<String, Double> interest = new HashMap<String, Double>();
        List<spot_intere> favor = new ArrayList<spot_intere>();
        Set<String> sourceSpots = AbstractDao.getRatedItems(source);
        Set<String> spots = new HashSet<String>();
        // Set<String> users=JdbcUtils.getAllUsers();
        Set<String> users = AbstractDao.getValuableUsers(source);
        int j = 0;
        for (String user : users) {
            if (j < 50) {
                if (!user.equals(source)) {
                    double sim = new similarity(source, user).computeSimilarity();
                    if (sim > 0) {
                        // System.out.println("sim: "+sim);
                        uslist.add(new user_sim(user, sim));
                        // System.out.println(user+" "+sim);
                    }
                }
                j++;
            } else {
                break;
            }
        }
        // System.out.println(uslist.size());
        Collections.sort(uslist, new mycomparator());
        int num = 0;
        for (user_sim us : uslist) {
            // System.out.println(us.getUser()+" "+us.getSim());
            if (num < 10) {
                temp_uslist.add(us);
                num++;
            } else {
                break;
            }
        }
        // System.out.println("hhh"+temp_uslist.size());
        for (user_sim uss : temp_uslist) {
            // System.out.println("zheli");
            String user = uss.getUser();
            Set<String> userSpots = AbstractDao.getRatedItems(user);
            // System.out.println("user"+user);
            // System.out.println(userSpots.size());
            for (String spot : userSpots) {
                // System.out.println("spot: "+spot);
                if (!sourceSpots.contains(spot)) {
                    spots.add(spot);
                }
            }
        }
        for (String spot : spots) {
            for (user_sim u : temp_uslist) {
                String user = u.getUser();
                int rating = AbstractDao.getRating(user, spot);
                if (rating > 0) {
                    if (!interest.containsKey(spot)) {
                        double value = u.sim * rating;
                        interest.put(spot, value);
                    }
                    if (interest.containsKey(spot)) {
                        double value = interest.get(spot) + u.sim * rating;
                        interest.put(spot, value);
                    }
                }
            }
        }
        Set<String> spotKey = interest.keySet();
        for (String key : spotKey) {
            favor.add(new spot_intere(key, interest.get(key)));
        }
        Collections.sort(favor, new mycomparator2());
        int i = 0;
        for (spot_intere spi : favor) {
            if (i < n) {
                // System.out.println(spi.getSpot()+" tuijian "+spi.getIntere());
                favoriteSpots.add(spi.getSpot());
                i++;
            } else {
                break;
            }
        }
        // System.out.println(favoriteSpots.size());
        return favoriteSpots;
    }

    public static void main(String[] args) {
        Set<String> spots = new UserCF("17608579", 10).getFavoriteSpot();
        for (String spot : spots) {
            System.out.println(spot);
        }
    }
}
